﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using System.IO;

/// <summary>
/// Summary description for ctp_lib
/// </summary>
public class ctp_lib
{
	public ctp_lib()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    //public enum news_type : int
    //{
    //    new_hotspot,new_ways_to_plan,new_intro,new_pic
    //}

    static public void check_user_language(System.Web.UI.Page page)
    {
        //we have only english version only 2007/12/13
        page.Session["now_user_lang_no"] = 0;

        if (page.Session["now_user_lang_no"] != null) return;

        string[] user_lang = page.Request.UserLanguages;



        ctpDataContext db = new ctpDataContext();
        var all_lang = from p in db.languages
                       where p.language_no == 0
                       select p;        

       

        int now_user_lang_no = -1;

        foreach (string u1 in user_lang)
        {
            if (now_user_lang_no == -1)
            {
                string u = u1;
                u = u.ToLower().Trim();
                if (u != "zh-tw")
                    u = (u.Split('-'))[0];

                foreach (language l in all_lang)
                {
                    if (u == l.code.Trim())
                    {
                        now_user_lang_no = l.no;
                    }
                }

            }
        }

        page.Session["now_user_lang_no"] = now_user_lang_no;

    }



    static public void savepic(System.Web.UI.Page page,string fn,HttpPostedFile file, string path)
    {

        string o_fn = path + "ori\\" + fn;
        string d_fn1 = path + "1\\" + fn;
        string d_fn2 = path + "2\\" + fn;
        string d_fn3 = path + "3\\" + fn;

        //file.SaveAs(o_fn,true);
        file.SaveAs(o_fn);



        //製作正方形縮圖
        System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
        Bitmap myBitmap = new Bitmap(o_fn);
        System.Drawing.Image myThumbnail = null;



        if (myBitmap.Width >= myBitmap.Height)
        {
            int w = (int)((75.0 / (float)myBitmap.Height) * (float)myBitmap.Width);

            if (w > 10000)
                w = 10000;

            int left = (w - 75) / 2;

            myThumbnail = myBitmap.GetThumbnailImage(w, 75, myCallback, IntPtr.Zero);
            Bitmap origi = new Bitmap(myThumbnail);
            Bitmap recThumb = new Bitmap(75, 75);

            for (int i = 0; i < 75; i++)
                for (int j = 0; j < 75; j++)
                {
                    recThumb.SetPixel(i, j, origi.GetPixel(left + i, j));
                }

            recThumb.Save(d_fn1, System.Drawing.Imaging.ImageFormat.Jpeg);

            //釋放進行縮圖處理的資源
            origi.Dispose();
            recThumb.Dispose();
        }
        else
        {
            int h = (int)((75.0 / (float)myBitmap.Width) * (float)myBitmap.Height);

            if (h > 10000)
                h = 10000;

            int top = (h - 75) / 2;

            myThumbnail = myBitmap.GetThumbnailImage(75, h, myCallback, IntPtr.Zero);
            Bitmap origi = new Bitmap(myThumbnail);
            Bitmap recThumb = new Bitmap(75, 75);

            for (int i = 0; i < 75; i++)
                for (int j = 0; j < 75; j++)
                {
                    recThumb.SetPixel(i, j, origi.GetPixel(i, top + j));
                }

            recThumb.Save(d_fn1, System.Drawing.Imaging.ImageFormat.Jpeg);

            //釋放進行縮圖處理的資源
            origi.Dispose();
            recThumb.Dispose();
        }


        //m size



        int target_w = 106;
        int target_h = 83;

        //若圖形較小，直接複製M，不製作縮圖
        if (myBitmap.Width <= target_w && myBitmap.Height <= target_h)
        {
            FileInfo fipic = new FileInfo(o_fn);
            //fipic.CopyTo(dir+user+"\\s-"+thumbname);
            fipic.CopyTo(d_fn2,true);
            

            myBitmap.Dispose();
            myThumbnail.Dispose();

            return;
        }

        //判斷類型作 m系列縮圖
        FileInfo testpic = new FileInfo(o_fn);



        //某些情況下，不製作M縮圖
        //圖檔比例超過一定範圍者
        if (((myBitmap.Width / myBitmap.Height) > 30) || ((myBitmap.Height / myBitmap.Width) > 30))
        {
            testpic.CopyTo(d_fn2,true);

            myBitmap.Dispose();
            myThumbnail.Dispose();

            return;
        }

        //20070505 製作縮圖品質提高到90
        if (testpic.Extension.ToLower().Equals(".jpg") || testpic.Extension.ToLower().Equals(".jpeg"))
        {
            //m系列.JPG格式.>50K
            testpic.CopyTo(d_fn2,true);

            if (myBitmap.Width <= 500 && myBitmap.Height <= 500)
            {
                PS.OnlineImageOptimizer.ImageOptimizer op = new PS.OnlineImageOptimizer.ImageOptimizer(d_fn2);
                op.Optimize(90);
            }
            else
            {
                PS.OnlineImageOptimizer.ImageOptimizer op = new PS.OnlineImageOptimizer.ImageOptimizer(d_fn2);
                op.Optimize(500, 500, 90);
            }
        }
        else
        {
            //m系列.非JPG格式
            //如果圖比500小就不作縮圖
            if (myBitmap.Width <= 500 && myBitmap.Height <= 500)
            {
                myBitmap.Save(d_fn2, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            else
            {
                if (myBitmap.Width > myBitmap.Height)
                {
                    int h = (int)((500.0 / (float)myBitmap.Width) * (float)myBitmap.Height);
                    myThumbnail = myBitmap.GetThumbnailImage(500, h, myCallback, IntPtr.Zero);
                }
                else
                {
                    int w = (int)((500.0 / (float)myBitmap.Height) * (float)myBitmap.Width);
                    myThumbnail = myBitmap.GetThumbnailImage(w, 500, myCallback, IntPtr.Zero);
                }
                myThumbnail.Save(d_fn2, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
        }


        //one more version, but now same size as 2
        FileInfo testpic1 = new FileInfo(d_fn2);
        testpic1.CopyTo(d_fn3,true);



        myBitmap.Dispose();
        myThumbnail.Dispose();
		

    }


    static bool ThumbnailCallback()
    {
        return false;
    } 


    static public void check_login(System.Web.UI.Page page,string backurl)
    {
        if (page.Session["userno"] == null)
        {
            page.Session["backurl"] = backurl;
            page.Response.Redirect("login.aspx");
        }

    }

    static public void check_login(System.Web.UI.Page page, string backurl,bool root)
    {
        if (page.Session["userno"] == null)
        {
            page.Session["backurl"] = backurl;

            if (root)
            {
                page.Response.Redirect("login.aspx");
            }
            else
            {
                page.Response.Redirect("/login.aspx");

            }
        }

    }

    static public void add_user_city(int userno, int ctno)
    {

        ctpDataContext db = new ctpDataContext();

        if (ctno == -1) return;

        int check = db.my_citiport_mines.Count(p => p.userno == userno && p.ct_no == ctno);
        if (check != 0) return;



        my_citiport_mine mi = new my_citiport_mine();
        mi.addtime = DateTime.Now;
        mi.ct_no = ctno;
        mi.userno = userno;
        mi.last_update_time = DateTime.Now;

        db.my_citiport_mines.InsertOnSubmit(mi);
        db.SubmitChanges();
       


    }



    //static public void insert_news(int userno, int ct_no, string descript, int type, string to_url, int to_url_no)
    //{
    //    ctpDataContext db = new ctpDataContext();
    //    ct_new my_news = new ct_new();




    //    ct_profile c = db.ct_profiles.Single(p => p.no == ct_no);

    //    my_news.addtime = DateTime.Now;
    //    my_news.country = c.country;
    //    my_news.create_user_no = userno;
    //    my_news.ct_no = ct_no;
    //    my_news.descript = descript;
    //    my_news.to_url = to_url;
    //    my_news.type = type;
    //    my_news.to_url_no = to_url_no;



    //}
}
